<?php
    require_once "bootstrap.php";


    function getUserInfoListAsJson()
    {
        global $entityManager;
        $userInfoArray = $entityManager->getRepository("UserInfo")->getUsersInfoAsArray();
        return json_encode($userInfoArray);
    }

    function addUserInfo($name, $sex, $notify, $comment)
    {
        global $entityManager;
        $userInfo = new UserInfo();
        $userInfo->setName($name);
        $userInfo->setSex($sex);
        $userInfo->setNotify($notify);
        $userInfo->setComment($comment);

        $entityManager->persist($userInfo);
        $entityManager->flush();
    }

    function deleteUserInfo($id)
    {
        global $entityManager;
        $user = $entityManager->find('UserInfo', $id);
        if ($user)
        {
            $entityManager->remove($user);
        }
        $entityManager->flush();
    }

    $id = (isset($_POST['id'])) ? $_POST['id'] : '';
    $name = (isset($_POST['name'])) ? $_POST['name'] : null;
    $sex = (isset($_POST['sex'])) ? $_POST['sex'] : null;
    $notify = (isset($_POST['notify'])) ? $_POST['notify'] : null;
    $comment = (isset($_POST['comment'])) ? $_POST['comment'] : null;

    if (!empty($id))
    {
        deleteUserInfo($id);
    }
    else if (!is_null($name) && !is_null($sex) && !is_null($notify) && !is_null($comment))
    {
        addUserInfo($name, $sex, $notify, $comment);
    }
    echo getUserInfoListAsJson();